package com.scs.application.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;
/**
 * @Description：调度相关配置
*/
@EnableAsync
@Configuration
public class AsyncConfig {

    @Bean
    public TaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);        // 设置核心线程数
        executor.setMaxPoolSize(10);        // 设置最大线程数
        executor.setQueueCapacity(20);      // 设置队列容量
        executor.setKeepAliveSeconds(60);   // 设置线程活跃时间（秒）
        executor.setThreadNamePrefix("async-");  // 设置默认线程名称
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());    // 设置拒绝策略
        executor.setWaitForTasksToCompleteOnShutdown(true); // 等待所有任务结束后再关闭线程池
        return executor;
    }



    @Value("${system.spd.fsdCorePoolSize:5}")
    int fsdCorePoolSize;
    @Value("${system.spd.fsdMaxPoolSize:10}")
    int fsdMaxPoolSize;
    @Value("${system.spd.fsdQueueCapacity:50}")
    int fsdQueueCapacity;
    @Bean(name="fsdTask")
    public TaskExecutor fsdtaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(fsdCorePoolSize);        // 设置核心线程数
        executor.setMaxPoolSize(fsdMaxPoolSize);        // 设置最大线程数
        executor.setQueueCapacity(fsdQueueCapacity);      // 设置队列容量
        executor.setKeepAliveSeconds(60);   // 设置线程活跃时间（秒）
        executor.setThreadNamePrefix("fsd_async-");  // 设置默认线程名称
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());    // 设置拒绝策略
        executor.setWaitForTasksToCompleteOnShutdown(true); // 等待所有任务结束后再关闭线程池
        return executor;
    }

}
